<template>
	<view class="">
		<view class="">
			<view class="u-p-x-50 flex flex-column align-center justify-center">
				<u-image class="u-m-t-50" width="200rpx" height="200rpx" borderRadius="20" src="https://ssjh.jifenma.com/assets/img/logo_gxxq.png"></u-image>
				<text class="u-p-y-20 u-font-26">Version: {{version}}</text>
			</view>
			<view class="u-p-x-50 u-m-t-40 bg-white" style="width: 100%;" @click="checkApp">
				<dy-cell-item leftTitle="版本更新"></dy-cell-item>
			</view>
		</view>
		<u-popup v-model="show" mode="center" borderRadius="10" :mask-close-able="false" @close="close">
			<view class="u-p-20 flex flex-column align-center justify-center" style="width: 400rpx;">
				<view class="" style="width: 100%;">
					<u-line-progress active-color="#2979ff" :percent="percent"></u-line-progress>
				</view>
				<text class="u-p-y-10">下载中...</text>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import { checkAppApi} from '@/api/checkApp.js'
	export default {
		data() {
			return {
				version: '',
				show: false,
				percent: 0,
				downloadTask: null,
				isCheck: false,
				wgtUrl:''
			}
		},
		onLoad(options) {
			this.version = getApp().globalData.version
			if(options.addr) {
				this.wgtUrl = options.app_update_package
				this.download()
			}
		},
		methods: {
			// 关闭蒙层 中断下载
			close() {
				console.log(this.downloadTask);
				if (this.downloadTask) {
					this.$Toast('取消下载安装!')
					this.downloadTask.abort()
					this.downloadTask = null
				}
			},
			// 检查更新
			checkApp() {
				// #ifdef H5
				this.$Toast('请在手机运行环境更新')
				// #endif
				// #ifdef APP-PLUS
				uni.showLoading()
				checkAppApi.getAppInfo().then(res => {
					uni.hideLoading()
					console.log('检查更新', res.data);
					console.log('当前版本', getApp().globalData.version);
					
					let newVersion = res.data.app_version.split('.')
					let oldVersion = getApp().globalData.version.split('.')
					console.log('newVersion', newVersion);
					console.log('oldVersion', oldVersion);
					this.wgtUrl = res.data.app_update_package
					if(newVersion[0]*1 > oldVersion[0]*1) return this.download()
					if(newVersion[1]*1 > oldVersion[1]*1) return this.download()
					if(newVersion[2]*1 > oldVersion[2]*1) return this.download()
					return this.$Toast('当前版本为最新版本!')
					
					// if (res.code == 1) {
					// 	let newVersion = res.data.split('.')
					// 	let oldVersion = getApp().globalData.version.split('.')
					// 	console.log('newVersion', newVersion);
					// 	console.log('oldVersion', oldVersion);
					// 	if(newVersion[0]*1 > oldVersion[0]*1) return this.download()
					// 	if(newVersion[1]*1 > oldVersion[1]*1) return this.download()
					// 	if(newVersion[2]*1 > oldVersion[2]*1) return this.download()
					// 	return this.$Toast('当前版本为最新版本!')
					// } else {
					// 	this.$Toast(res.msg)
					// }
				});
				// #endif
			},
			// 下载更新资源
			download() {
				this.show = true
				console.log('下载更新资源', checkAppApi.wgtUrl_Api);
				this.downloadTask = uni.downloadFile({
					url: this.$_BASE_IMG_API + this.wgtUrl,
					success: (downloadResult) => {
						if (downloadResult.statusCode === 200) {
							plus.runtime.install(downloadResult.tempFilePath, {force: false}, () => {
								this.isShow = false
								this.$Toast('下载更新完成')
								console.log('install success...');
								plus.runtime.restart();
							}, (e) => {
								this.isShow = false
								this.$Toast('下载更新失败')
								console.error('install fail...', e);
							});
						}
					},
					fail: (err) => {
						this.$Toast('下载失败')
						console.log(err);
					}
				})
				this.downloadTask.onProgressUpdate((res) => {
					this.percent = res.progress
				});
			},
		}
	}
</script>

<style lang="scss" scoped>

</style>
